package org.csu.jpetstore_user.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.csu.jpetstore_user.entity.*;
import org.csu.jpetstore_user.persistence.ItemMapper;
import org.csu.jpetstore_user.persistence.ProductMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

@Controller
//@MapperScan("org.csu.jpetstore_user.persistence")
public class CatalogController {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    //主页显示信息
    /**
     * 授权回调
     */
    @GetMapping("/mainForm")
    public String home(HttpServletRequest request,HttpSession session, Model model) throws IOException {


        //显示宠物信息
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("category","DOGS");
        List<Product> productList = productMapper.selectList(productQueryWrapper);

        QueryWrapper<Product> productQueryWrapper2 = new QueryWrapper<>();
        productQueryWrapper2.eq("category","CATS");
        List<Product> productList2 = productMapper.selectList(productQueryWrapper2);

        QueryWrapper<Product> productQueryWrapper3 = new QueryWrapper<>();
        productQueryWrapper3.eq("category","BIRDS");
        List<Product> productList3 = productMapper.selectList(productQueryWrapper3);

        QueryWrapper<Product> productQueryWrapper4 = new QueryWrapper<>();
        productQueryWrapper4.eq("category","REPTILES");
        List<Product> productList4 = productMapper.selectList(productQueryWrapper4);

        QueryWrapper<Product> productQueryWrapper5 = new QueryWrapper<>();
        productQueryWrapper5.eq("category","FISH");
        List<Product> productList5 = productMapper.selectList(productQueryWrapper5);

        //将宠物信息列表放入session中
        session.setAttribute("productList",productList);
        session.setAttribute("productList2",productList2);
        session.setAttribute("productList3",productList3);
        session.setAttribute("productList4",productList4);
        session.setAttribute("productList5",productList5);
        return "catalog/main";
    }

    //首页点击详情按钮进入productForm
    @GetMapping("/productForm")
    public String productForm(@RequestParam("productId") String productId,HttpSession session){
        System.out.println("进入productForm......");
        System.out.println(productId);
        //封装对应的宠物product信息
        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        productQueryWrapper.eq("productId",productId);
        Product product = productMapper.selectOne(productQueryWrapper);
        session.setAttribute("product",product);

        QueryWrapper<Item> itemQueryWrapper = new QueryWrapper<>();
        itemQueryWrapper.eq("productId",productId);
        List<Item> itemList = itemMapper.selectList(itemQueryWrapper);
        session.setAttribute("itemList",itemList);
//        System.out.println(itemList.get(1));
        //跳转页面
        return "catalog/product";
    }

    //用户退出登录
    @GetMapping("/signOut")
    public String signOut(HttpSession session){
        session.removeAttribute("loginUser");
        session.removeAttribute("account");
        return "catalog/main";
    }

    //搜索进入searchProducts
    @PostMapping("/search")
    public String search(@RequestParam("keyword") String keyword, @Validated @ModelAttribute Product product, HttpSession session) {

        System.out.println("Search...");
        System.out.println(keyword);
        QueryWrapper<Product> searchProductQueryWrapper = new QueryWrapper<>();
        searchProductQueryWrapper.like("name",keyword);

        List<Product> searchProductList = productMapper.selectList(searchProductQueryWrapper);
        session.setAttribute("searchProductList",searchProductList);
        System.out.println(searchProductList);

//        Account account = (Account) session.getAttribute("account");


//        Account account = (Account) session.getAttribute("account");
//
//        if (account != null) {
//            String strBackUrl = "http://" + request.getServerName() + ":" + request.getServerPort()
//                    + request.getContextPath() + request.getServletPath() + "?" + (request.getQueryString());
//
//            LogService logService = new LogService();
//            String logInfo = logService.logInfo(" ") + strBackUrl + " 查找商品" + "  " + productList;
//            logService.insertLogInfo(account.getUsername(), logInfo);
//
//        }
//        request.getRequestDispatcher(SEARCH).forward(request, response);

        return "catalog/searchProducts";

    }


}
